


class CategoriesController < ApplicationController
  before_filter :authenticate_member
  before_filter :authenticate_admin, :except => ['show']
  
  # GET /categories
  # GET /categories.xml
  def index
    @categories = Category.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @categories }
    end
  end

  # GET /categories/1
  # GET /categories/1.xml
  def show
    @category = Category.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @category }
    end
  end

  def svn_update
    ret = `svn update #{RAILS_ROOT}`
    ret << `rake -f #{RAILS_ROOT}/Rakefile db:migrate`
    flash[:notice] = ret
    `touch #{RAILS_ROOT}/tmp/restart.txt`
    redirect_to '/'
  end

  # GET /categories/new
  # GET /categories/new.xml
  def new
    @category = Category.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @category }
    end
  end

  # GET /categories/1/edit
  def edit
    @category = Category.find(params[:id])
  end

  # POST /categories
  # POST /categories.xml
  def create
    @category = Category.new(params[:category])

    respond_to do |format|
      if @category.save
        flash[:notice] = 'Category was successfully created.'
        format.html { redirect_to(@category) }
        format.xml  { render :xml => @category, :status => :created, :location => @category }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /categories/1
  # PUT /categories/1.xml
  def update
    @category = Category.find(params[:id])
    params[:category][:img_url] = ''
    respond_to do |format|
      if @category.update_attributes(params[:category])
        flash[:notice] = 'Category was successfully updated.'
        format.html { redirect_to(categories_url) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.xml
  def destroy
    @category = Category.find(params[:id])
    @category.destroy

    respond_to do |format|
      format.html { redirect_to(categories_url) }
      format.xml  { head :ok }
    end
  end

  def update_rss
    if params[:id] && !params[:id].empty? && categ = Category.find(params[:id].to_i)
      categ.update_rss
      redirect_to category_medias_path(categ)
    else
      flash[:notice] = "La requête est invalide, la catégorie n'a pu être mise à jour !"
    end
  end

  def update_all_rss
    ROOTS_CATEGORIES.each do |cat|
        puts "mise a jour de categorie #{cat.label}"
        cat.update_rss
    end
    redirect_to '/'
  end


end
